<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Siteorder_model extends My_Model
{    
    public function __construct()
    {
        parent::__construct();
        $this->model_name="网站订单";
        $this->tablename="sites_order";  
    }
        



    public function getbyfilter($siteid,$key="",$uid=0,$state=0,$timelimit=[],$except=0)
    {
        $where ="1=1";
        if($siteid)
        {
            if(is_array($siteid))
            {
                $where.=" and siteid in (".implode(",",$siteid).")";
            }
            else
            {
                $where.=" and siteid=".$siteid;
            }
        }
        if($uid>0)
        {
            $where.=" and uid=".$uid;
        }
        if($state>0)
        {
            $where.=" and state=".$state;
        }

        if($timelimit && isset($timelimit["start"]) && isset($timelimit["end"]))
        {
            $where.=" and addtime between '".$timelimit["start"]." 00:00:00' and '".$timelimit["end"]." 23:59:59'";
        }
        
        if($except==1)
        {
            $where.=" and buyamount>0 and (amount-buyamount-shippingfee)<10";
        }
        if($key)
        {
            $skey=[
                "buy_ordersn"=>$key,
                "ordersn"=>$key,
                "realname"=>$key,
                "email"=>$key,
                "note"=>$key,
                "productinfo"=>$key,
                "amount"=>$key,
                "address1"=>$key,
                "shipping"=>$key,
                "zipcode"=>$key,
                "extra"=>$key,
            ];
        }
        else
        {
            $skey="";
        }
        return $this->get_items_by_where($where);
    }

    
    public function queryorder($key,$type)
    {
        $where[$type]=$key;
        return $this->get_items_by_where($where);
    }

    public function getpage($siteid,$page,$pagesize,$key="",$uid=0,$state,$timelimit=[],$except=0,$shopfrom="",$sort=""){
        
        $where ="1=1";

        if($pagesize>0)
        {
            $this->pagesize=$pagesize;
        }
        
        if($siteid)
        {
            if(is_array($siteid))
            {
                $where.=" and siteid in (".implode(",",$siteid).")";
            }
            else
            {
                $where.=" and siteid=".$siteid;
            }
        }

        $url=uri_string();
        if($uid>0)
        {
            $where.=" and uid=".$uid;
        }
        if($state!=0)
        {
            $where.=" and state=".$state;
        }else{
            $where.=" and state= 0";
        }
        if($shopfrom)
        {
            $where.=" and buy_source='{$shopfrom}'";
        }

        if($timelimit && isset($timelimit["start"]) && isset($timelimit["end"]))
        {
            $where.=" and addtime between '".$timelimit["start"]." 00:00:00' and '".$timelimit["end"]." 23:59:59'";
        }
        
        if($except==1)
        {
            $where.=" and buyamount>0 and (amount-buyamount)<10";
        }
        if($key)
        {
            $skey=[
                //"buy_ordersn"=>$key,
                //"ordersn"=>$key,
                "siteid"=>$key,
                "quantity"=>$key,
                "phone"=>$key,
                "realname"=>$key,
                "email"=>$key,
                "note"=>$key,
                "country"=>$key,
                //"productinfo"=>$key,
                //"amount"=>$key,
                //"address1"=>$key,
                //"shipping"=>$key,
                //"zipcode"=>$key,
                //"extra"=>$key,
            ];
        }
        else
        {
            $skey="";
        };

        if($sort==0){
            $desc = "addtime";
        }else{
            $desc = "id";
        };
        return $this->get_items_by_page($where,$page,$url,$desc." desc",[],$skey);
    }


    public function getordercount($siteid)
    {
        $where=["siteid"=>$siteid];
        $count=$this->getcount($where);
        return $count;
    }

    public function checkuserordercount($order)
    {
        if($order["uid"]==0)
        {
            return 0;
        }
        $starttime = date("Y-m-d 00:00:00",strtotime("-2 days",strtotime($order["addtime"])));
        $endtime =  date("Y-m-d 00:00:00",strtotime("+1 days",strtotime($order["addtime"])));
        $where="uid=".$order["uid"]." and siteid=".$order["siteid"]." and addtime between '{$starttime}' and '{$endtime}'";

        $count = 1;
        $orders = $this->get_items_by_where($where);
        foreach($orders as $item){
            if($item["id"]!=$order["id"] && $item["address1"]==$order["address1"])
            {
                $count++;
            }
        }
        return $count;
    }
    

    public function checkcategory($siteid,$rid)
    {


    }

    public function getrangesum($timelimit,$siteid=0)
    {
        if($timelimit && isset($timelimit["start"]) && isset($timelimit["end"]))
        {
            $where=" state not in (-1) and addtime between '".$timelimit["start"]." 00:00:00' and '".$timelimit["end"]." 23:59:59'";
            if ($siteid)
            {
                if(is_array($siteid))
                {
                    $where.=" and siteid in (".implode(",",$siteid).")";
                }
                else
                {
                    $where.=" and siteid=".$siteid;
                }
            }
            //$this->db->select_sum("goodcount","goodcounts");
            //$this->db->select_sum("amount","totalamount");
            $query=$this->db->select('count(*) as cnt')->where($where)->get($this->table_prefix.$this->tablename);    
            $result=$query->row_array();
            return $result;
        }
        else
        {
            return false;
        }
    }

    public function tongji($days)
    {   
        $total=[
            "ordercount"=>0,
        ];
        $result=[];
        foreach($days as $day)
        {
            $item=[
                "day"=>$day,
                "ordercount"=>0,
            ];
            $where="state not in (-1) and datediff ( addtime , '{$day}' ) = 0";
            $list=$this->get_items_by_where($where);
            if($list){
                $item["ordercount"]=count($list);
              
                $total["ordercount"]+=$item["ordercount"];
              
            }

            $result[]=$item;
        }
        return ["data"=>$result,"total"=>$total];
    }


    public function getquery()
    {
        $where= "buy_source='aliexpress' and buy_ordersn!='' and (shipping='' or (siteid!=26 and shipcount>0))";
        return $this->get_items_by_where($where);
    }

    public function getbyshipping($shippnum)
    {
        $where["shipping"]=$shippnum;
        return $this->get_item_by_where($where);
    }

    public function getby1688oid($oid)
    {
        $where="buy_source='1688' and buy_ordersn like '%".$oid."%'";
        return $this->get_item_by_where($where);
    }

    public function getbyalioid($oid)
    {
        $where="buy_source='aliexpress' and buy_ordersn like '%".$oid."%'";
        return $this->get_item_by_where($where);
    }
    
    public function gettongji(){

        $where=" state not in (1,6,7,8) and tongji=0";
        return $this->get_items_by_where($where);                
    }

    public function getcancel()
    {
        $where=" state=6 and tongji=1";
        return $this->get_items_by_where($where);     
    }
    
    public function getall(){
        
        $where=[];

        return $this->get_items_by_where($where);                
    }

    public function delitem($id)
    {
        $where=["id"=>$id];
        $this->delete($where);
        return true;
    }

    public function earorder($siteid)
    {
        $where='siteid='.$siteid.' and state in (1,8,9)';
        return $this->get_items_by_where($where);
    }
    
    public function getitembyid($id){
        $where=array("id"=>$id);
  
        return $this->get_item_by_where($where);
    }

    public function getitembyids($ids)
    {
        $where = " id in (".implode(",",$ids).")";
        return $this->get_items_by_where($where);
    }

    public function getitembyordersn($ordersn){
        $where=array("ordersn"=>$ordersn);
  
        return $this->get_item_by_where($where);
    }

    public function getfilter($where)
    {
        return $this->get_items_by_where($where);
    }
    
    public function create($data){
        if(!isset($data["addtime"])){
            $data["addtime"]=date("Y-m-d H:i:s");
        }
       
        return $this->add_item($data);
    }

    public function update($id,$data){

        if(is_array($id))
        {
            $where="id in (".implode(",",$id).")";
           
        }else{
            $where=array("id"=>$id);
           
        }
        return $this->update_item($where,$data);

    }

   
}
